<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    width="75%"
    height="60%"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >

<!--      <el-form-item label="回答问题" prop="problemTitle">-->
<!--        <el-input-->
<!--          v-model="dataForm.problemTitle"-->
<!--          placeholder="回答问题"-->
<!--          :disabled="true"-->
<!--        ></el-input>-->
<!--      </el-form-item>-->


      <el-form-item label="回答内容" prop="answerContent" style="margin-bottom: 100px">
        <div >
          <div  style="width: 100%;">
            <quill-editor v-model="dataForm.answerContent" style="height: 250px"></quill-editor>
          </div>
        </div>
      </el-form-item>





    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { quillEditor } from 'vue-quill-editor';
import 'quill/dist/quill.core.css'; // import styles
import 'quill/dist/quill.snow.css'; // for snow theme
import 'quill/dist/quill.bubble.css'; // for bubble theme
export default {
  components: {
    quillEditor
  },
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        answerContent: '',
        problemTitle: '',
        teacherName: '',
        createTime: '',
        problemDescription: '',
        updateTime: '',
        isDelete: ''
      },
      dataRule: {
        answerContent: [
          { required: true, message: '回答内容不能为空', trigger: 'blur' }
        ],
        problemTitle: [
          { required: true, message: '回答的问题不能为空', trigger: 'blur' }
        ],
        teacherName: [
          { required: true, message: '回答的讲师不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: false, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: false, message: '修改时间不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/problem/problemanswer/info/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.answerContent = data.problemAnswer.answerContent;
              this.dataForm.problemTitle = data.problemAnswer.problemTitle;
              this.dataForm.teacherName = data.problemAnswer.teacherName;
              this.dataForm.createTime = data.problemAnswer.createTime;
              this.dataForm.updateTime = data.problemAnswer.updateTime;
              this.dataForm.isDelete = data.problemAnswer.isDelete;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/problem/problemanswer/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              answerContent: this.dataForm.answerContent,
              problemTitle: this.dataForm.problemTitle,
              problemDescription: this.dataForm.problemDescription,
              teacherName: this.dataForm.teacherName,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              isDelete: this.dataForm.isDelete
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit('refreshDataList');
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
